﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonInterfaces.Models;
using System.Windows.Controls;
using System.Windows.Input;
using GUI.Custom_Classes;
using System.Windows;

namespace StandardScriptItems.SimpleControls
{
    public class ElementStateComboBox : EditableComboBox
    {
        public delegate void ElementSelectedDelegate(String element);
        public event ElementSelectedDelegate ElementSelectionChanged;

        private IElementState state;

        public string[] currentPath;
        public int currentIndex = -1;

        public ElementStateComboBox()
            : base("--Which Element?--", new List<String>())
        {
        }

        public ElementStateComboBox(IElementState istate)
            : this(istate, "--Which Element?--")
        { }

        public ElementStateComboBox(IElementState istate, String src)
            : base(src, istate.Elements)
        {
            Name = "ElementSelector";
            state = istate;
            if(state.elementLocation != null)
            {
                currentPath = state.fullPath.ToArray();
                ElementSelectionChanged += delegate(String str) { };
                SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
                {
                    string elem = (SelectedItem as ComboBoxItem).Content.ToString();
                    ElementSelectionChanged(elem);
                    currentIndex = PathIndex(elem);
                };
            }
        }

        public override ComboBoxItem Add(object obj)
        {
            ComboBoxItem item = base.Add(obj);

            item.MouseMove += delegate(object sender, MouseEventArgs args)
            {
                try
                {
                    if (item.Content is String)
                    {
                        Rect rect = state.GetAELocation(item.Content.ToString());
                        Hilighter_Box.Singleton.Target(rect);
                    }
                }
                catch (Exception e) { /*Couldn't get the element. Alas, we can not highlight it.*/ }
            };

            item.MouseLeave += delegate(object sender, MouseEventArgs args)
            {
                Hilighter_Box.Singleton.Hide();
            };

            return item;
        }

        public int PathIndex(string selected)
        {
            return state.Elements.ToList().IndexOf(selected);
        }

        public override string ToString()
        {
            string val = (SelectedItem as ComboBoxItem).Content.ToString();
            if (val.Contains("System.Windows.Controls.TextBox:"))
                val = val.Remove(0, "System.Windows.Controls.TextBox:".Count()).Trim();
            currentIndex = state.IndexOf(val);

            if (currentIndex != -1)
                return currentPath[currentIndex];
            return base.ToString();
        }

        public void Filter(LEET.CommonInterfaces.Filter<String> test)
        {
            Clear();
            foreach (String elem in state.Elements)
            {
                if (test(elem))
                {
                    Add(elem);
                }
            }
        }
    }
}
